package com.itheima.ai.memory;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ai.mapper.ConversationMapper;
import com.itheima.ai.pojo.Conversation;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.messages.AssistantMessage;
import org.springframework.ai.chat.messages.Message;
import org.springframework.ai.chat.messages.MessageType;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

@Component
public class InMysqlChatMemory implements ChatMemory {


    @Autowired
    private ConversationMapper conversationMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void add(String conversationId, List<Message> messages) {
        for (Message message : messages){
            Conversation conversation = Conversation.builder()
                    .conversationId(conversationId)
                    .type(message.getMessageType().getValue())
                    .message(message.getText())
                    .createTime(LocalDateTime.now())
                    .build();
            redisTemplate.opsForList().leftPush(conversationId,conversation);
            //conversationMapper.insert(conversation);
        }


    }

    @Override
    public List<Message> get(String conversationId, int lastN) {
        //1.根据条件查询数据库表
//        IPage<Conversation> page=new Page<>(1,lastN);
//        IPage<Conversation> conversationIPage = conversationMapper.selectPage(page,
//                Wrappers.<Conversation>lambdaQuery().eq(Conversation::getConversationId,conversationId));
        //2.获取查询列表
//        List<Conversation> records = conversationIPage.getRecords();


        //从Redis中取出数据
        List<Conversation> records = redisTemplate.opsForList().range(conversationId,  -lastN, -1);


        if (!CollectionUtils.isEmpty(records)) {
            Collections.reverse(records);
        }

        //3.封装会话列表
        if (CollectionUtils.isEmpty(records)) {
            return List.of();
        }else{
            return records.stream().map(conversation ->{
                Message message=null;
                if(MessageType.ASSISTANT.getValue().equals(conversation.getType())){
                    message=new AssistantMessage(conversation.getMessage());
                }else if (MessageType.USER.getValue().equals(conversation.getType())){
                    message=new UserMessage(conversation.getMessage());
                }
                return message;
            }).toList();
        }
    }

    @Override
    public void clear(String conversationId) {
        conversationMapper.delete(Wrappers.<Conversation>lambdaQuery().eq(Conversation::getConversationId,conversationId));
    }
}
